﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DAL.Service.Impl;
using DAL.Service;
using DomainModel;

namespace BIZ
{
   public class UserOper
    {
       IUserInfoService userService = new UserInfoServiceImpl();

       public IList getUserInfolist(string nick, string name, string enname)
       {
           return userService.getUserInfolist(nick, name, enname);
       }

       public IList getUserInfoByCard(string nick, string name, string card)
       {
           return userService.getUserInfoByCard(nick, name, card);
       }

       public object save(object entity)
       {
           return userService.save(entity);
       }

       /// <summary>
       /// 验证用户名和学籍编号是否存在
       /// </summary>
       /// <param name="nick"></param>
       /// <param name="number"></param>
       /// <returns></returns>
       public int reg(string nick, string number,string uid)
       {
           IList liNick= userService.regNick(nick,uid);
           IList liNumber = userService.regNumber(number,uid);
           if (liNick.Count > 0 && liNumber.Count > 0)
           {
               return 1;//用户名和学籍编号都已存在
           }
           else if (liNick.Count>0)
           {
               return 2;//用户名存在
           }
           else if (liNumber.Count > 0)
           {
               return 3;//学籍编号存在
           }
           else
           {
               return 0;//可以新增
           }
       }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="uname">中文名</param>
       /// <param name="unick">登录名</param>
       /// <param name="unumber">学籍编号</param>
       /// <param name="ujwclass">嘉沃班级</param>
       /// <param name="uteacher1">教师1</param>
       /// <param name="uteacher2">教师2</param>
       /// <param name="ustudyTime1">上课时间1</param>
       /// <param name="ustudyTime2">上课时间2</param>
       /// <param name="uregistTime_s">注册时间s</param>
       /// <param name="uregistTime_e">注册时间e</param>
       /// <param name="pageno">当前页</param>
       /// <param name="pagesize">总页</param>
       /// <returns></returns>
       public IList getUserinfoList(string uname, string unick, string unumber, string ujwclass, string uteacher1, string ucard, string uvip, string ustate, string uregistTime_s, string uregistTime_e, int pageno, int pagesize,string orderName,string orderEname,string orderNumer,string orderBrithday,string orderVip,string orderJwclass,string orderId)
       {
           return userService.getUserinfoListSearch(uname, unick, unumber, ujwclass, uteacher1, ucard, uvip, ustate, uregistTime_s, uregistTime_e, pageno, pagesize,orderName,orderEname,orderNumer,orderBrithday,orderVip,orderJwclass,orderId);
       }

       public IList getuserinfobyid(int uid)
       {
           return userService.getuserinfobyid(uid);
       }

       public int updatepass(int uid)
       {
           return userService.updatepass(uid);
       }

       public int cuvip(string uid, string uvip)
       {
           return userService.cuvip(uid, uvip);
       }

       public IList getUserCardList(int uid)
       {
           return userService.getUserCardList(uid);
       }
       public IList userBooksList(int uid)
       {
           return userService.userBooksList(uid);
       }
       /// <summary>
       /// 登录、登出日志 
       /// </summary>
       public void saveLoginLog(int uid,string ip,int type,string remark,int from)
       {
           LoginLog ll = new LoginLog();
           ll.ll_id = 0;
           ll.ll_uid = uid;
           ll.ll_type = type;
           ll.ll_ip = ip;
           ll.ll_remark = remark;
           ll.ll_from = from;
           ll.ll_date = DateTime.Now;

           userService.save(ll);
       }
       public int userlogin(string uname, string upass,string ip,int from,ref UserInfo user)
       {
           IList list = userService.getUserInfo(uname, BIZ.StringTool.md5(upass,16));
           if (list.Count <= 0)
               return (int)DAL.Enums.loginState.用户名或密码错误;
           Hashtable tb = (Hashtable)list[0];
           UserInfo u = new UserInfo();
           if (tb["u_id"] != null) u.u_id = int.Parse(tb["u_id"].ToString());
           if (tb["u_nick"] != null) u.u_nick = tb["u_nick"].ToString();
           if (tb["u_pass"] != null) u.u_pass = tb["u_pass"].ToString();
           if (tb["u_from"] != null) 
           { 
              if (tb["u_from"].ToString() != "") u.u_from = byte.Parse(tb["u_from"].ToString());
           }
           if (tb["u_registTime"] != null)
           {
               if (tb["u_registTime"].ToString()!="") u.u_registTime = DateTime.Parse(tb["u_registTime"].ToString());
           }
               
           if (tb["u_number"] != null) u.u_number = tb["u_number"].ToString();
           if (tb["u_name"] != null) u.u_name = tb["u_name"].ToString();
           if (tb["u_enName"] != null) u.u_enName = tb["u_enName"].ToString();
           if (tb["u_jwclass"] != null) 
           { 
                if(tb["u_jwclass"].ToString()!="") u.u_jwclass = int.Parse(tb["u_jwclass"].ToString());
           } 
           if (tb["u_papers"] != null) u.u_papers = tb["u_papers"].ToString();
           if (tb["u_birthday"] != null)
           {
               if (tb["u_birthday"].ToString() != "") u.u_birthday = DateTime.Parse(tb["u_birthday"].ToString());
           }
           if (tb["u_age"] != null)
           {
               if (tb["u_age"].ToString() != "") u.u_age = int.Parse(tb["u_age"].ToString());
           }
           if (tb["u_sex"] != null)
           {
               if (tb["u_sex"].ToString() != "") u.u_sex = byte.Parse(tb["u_sex"].ToString());
           } 
           if (tb["u_school"] != null) u.u_school = tb["u_school"].ToString();
           if (tb["u_class"] != null) u.u_class = tb["u_class"].ToString();
           if (tb["u_english"] != null) u.u_english = tb["u_english"].ToString();
           if (tb["u_patriarchName1"] != null) u.u_patriarchName1 = tb["u_patriarchName1"].ToString();
           if (tb["u_raletion1"] != null) u.u_raletion1 = tb["u_raletion1"].ToString();
           if (tb["u_tel1"] != null) u.u_tel1 = tb["u_tel1"].ToString();
           if (tb["u_patriarchAge1"] != null)
           {
               if (tb["u_patriarchAge1"].ToString() != "") u.u_patriarchAge1 = int.Parse(tb["u_patriarchAge1"].ToString());
           }
           if (tb["u_patriarchEducation1"] != null) u.u_patriarchEducation1 = tb["u_patriarchEducation1"].ToString();
           if (tb["u_job1"] != null) u.u_job1 = tb["u_job1"].ToString();
           if (tb["u_post1"] != null) u.u_post1 = tb["u_post1"].ToString();
           if (tb["u_patriarchName2"] != null) u.u_patriarchName2 = tb["u_patriarchName2"].ToString();
           if (tb["u_raletion2"] != null) u.u_raletion2 = tb["u_raletion2"].ToString();
           if (tb["u_tel2"] != null) u.u_tel2 = tb["u_tel2"].ToString();
           if (tb["u_patriarchAge2"] != null)
           {
               if (tb["u_patriarchAge2"].ToString() != "") u.u_patriarchAge2 = int.Parse(tb["u_patriarchAge2"].ToString());
           }
           if (tb["u_patriarchEducation2"] != null) u.u_patriarchEducation2 = tb["u_patriarchEducation2"].ToString();
           if (tb["u_job2"] != null) u.u_job2 = tb["u_job2"].ToString();
           if (tb["u_post2"] != null) u.u_post2 = tb["u_post2"].ToString();
           if (tb["u_address"] != null) u.u_address = tb["u_address"].ToString();
           if (tb["u_email"] != null) u.u_email = tb["u_email"].ToString();
           if (tb["u_state"] != null)
           {
               if (tb["u_state"].ToString() != "") u.u_state = byte.Parse(tb["u_state"].ToString());
           } 
           if (tb["u_time"] != null)
           {
               if (tb["u_time"].ToString() != "") u.u_time = DateTime.Parse(tb["u_time"].ToString());
           }
           if (tb["u_teacher1"] != null) u.u_teacher1 = tb["u_teacher1"].ToString();
           if (tb["u_teacher2"] != null) u.u_teacher2 = tb["u_teacher2"].ToString();
           if (tb["u_studyTime1"] != null) u.u_studyTime1 = tb["u_studyTime1"].ToString();
           if (tb["u_studyTime2"] != null) u.u_studyTime2 = tb["u_studyTime2"].ToString();
           if (tb["u_vip"] != null)
           {
               if (tb["u_vip"].ToString() != "") u.u_vip = int.Parse(tb["u_vip"].ToString());
           } 
           if (tb["u_vipUpdateTime"] != null)
           {
               if (tb["u_vipUpdateTime"].ToString() != "") u.u_vipUpdateTime = DateTime.Parse(tb["u_vipUpdateTime"].ToString());
           }
           if (tb["u_user"] != null)
           {
               if (tb["u_user"].ToString() != "") u.u_user = int.Parse(tb["u_user"].ToString());
           } 
           if (tb["u_addTime"] != null)
           {
               if (tb["u_addTime"].ToString() != "") u.u_addTime = DateTime.Parse(tb["u_addTime"].ToString());
           }
           if (tb["u_lastUser"] != null)
           {
               if (tb["u_lastUser"].ToString() != "") u.u_lastUser = int.Parse(tb["u_lastUser"].ToString());
           }
           if (tb["u_updateTime"] != null)
           {
               if (tb["u_updateTime"].ToString() != "") u.u_updateTime = DateTime.Parse(tb["u_updateTime"].ToString());
           }

           if (u.u_vip == (int)DAL.Enums.uservip.非会员黑户)
               return (int)DAL.Enums.loginState.黑户;
           user = u;
           //写登录日志 
           saveLoginLog(u.u_id, ip, (int)DAL.Enums.useropertype.登录, null, from);
           return (int)DAL.Enums.loginState.登录成功;
       }

       /// <summary>
       /// 收藏图书
       /// </summary>
       /// <param name="dlid"></param>
       /// <param name="lbid"></param>
       public void saveUserSave(int dlid, int lbid)
       {
           DomainModel.UserSave us = new UserSave();
           us.us_dlid = dlid;
           us.us_lbid = lbid;
           us.us_time = DateTime.Now;
           save(us);
       }
       public bool checkUserSave(int bookid, int uid)
       {
           return userService.getUserSaveCount(bookid, uid) > 0 ? true : false;
       }
    }

}
